home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / vbdatabs / strdb.cpp < prev    next >
C/C++ Source or Header  |  1999-03-17  |  11KB  |  411 lines

  1. // ------------------------------- //
  2. // -------- Start of File -------- //
  3. // ------------------------------- //
  4. // ----------------------------------------------------------- // 
  5. // C++ Source Code File Name: strdb.cpp 
  6. // Compiler Used: MSVC40, DJGPP 2.7.2.1, GCC 2.7.2.1, HP CPP 10.24
  7. // Produced By: Doug Gaer   
  8. // File Creation Date: 09/17/1997  
  9. // Date Last Modified: 03/18/1999
  10. // Copyright (c) 1997 Douglas M. Gaer
  11. // ----------------------------------------------------------- // 
  12. // ------------- Program Description and Details ------------- // 
  13. // ----------------------------------------------------------- // 
  14. /*
  15. The VBD C++ classes are copyright (c) 1997, by Douglas M. Gaer.
  16. All those who put this code or its derivatives in a commercial
  17. product MUST mention this copyright in their documentation for
  18. users of the products in which this code or its derivative
  19. classes are used. Otherwise, you have the freedom to redistribute
  20. verbatim copies of this source code, adapt it to your specific
  21. needs, or improve the code and release your improvements to the
  22. public provided that the modified files carry prominent notices
  23. stating that you changed the files and the date of any change.
  24.  
  25. THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND.
  26. THE ENTIRE RISK OF THE QUALITY AND PERFORMANCE OF THIS SOFTWARE
  27. IS WITH YOU. SHOULD ANY ELEMENT OF THIS SOFTWARE PROVE DEFECTIVE,
  28. YOU WILL ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR
  29. CORRECTION.
  30.  
  31. The StrDB class is a general purpose string database used to
  32. represent one unique key member, 7 general purpose members,
  33. and one comment block. All the string members can grow and
  34. shrink as needed
  35. */
  36. // ----------------------------------------------------------- //   
  37. #include "strdb.h"
  38. #include "strutil.h"
  39.  
  40. unsigned StrDB::ObjectLength()
  41. {
  42.   unsigned len = StringFileLength(KM) + \
  43.     StringFileLength(M2) + \
  44.     StringFileLength(M3)  + \
  45.     StringFileLength(M4)  + \
  46.     StringFileLength(M5)  + \
  47.     StringFileLength(M6)  + \
  48.     StringFileLength(M7)  + \
  49.     StringFileLength(M8)  + \
  50.     StringFileLength(CM);
  51.  
  52.   return len;
  53. }
  54.  
  55. FAU StrDB::Write()
  56. {
  57.   ObjectHeader oh;
  58.  
  59.   FAU addr = pod->OpenDatabase()->Alloc(ObjectLength() + sizeof(ObjectHeader));
  60.   if(!addr) return 0;
  61.   
  62.   oh.ClassID = GetClassID();
  63.   oh.ObjectID = addr;
  64.   WriteObjHeader(oh, addr);
  65.   WriteString(KM);
  66.   WriteString(M2);
  67.   WriteString(M3);
  68.   WriteString(M4);
  69.   WriteString(M5);
  70.   WriteString(M6);
  71.   WriteString(M7);
  72.   WriteString(M8);
  73.   WriteString(CM);
  74.   objectaddress = addr;  
  75.  
  76.   // Add the entry to the Index file
  77.   if (UsingIndex()) {
  78.     EntryKey key(KM.c_str(), oh.ObjectID, oh.ClassID);
  79.     AddKey(key);
  80.   }
  81.   return addr;
  82. }
  83.  
  84. void StrDB::Read(FAU Address)
  85. {
  86.   ObjectHeader oh;
  87.  
  88.   ReadObjHeader(oh, Address);
  89.   if(oh.ClassID != GetClassID()) return; // Incorrect object type
  90.  
  91.   ReadString(KM);
  92.   ReadString(M2);
  93.   ReadString(M3);
  94.   ReadString(M4);
  95.   ReadString(M5);
  96.   ReadString(M6);
  97.   ReadString(M7);
  98.   ReadString(M8);
  99.   ReadString(CM);
  100.   objectaddress = Address;
  101. }
  102.  
  103. FAU StrDB::Find()
  104. // Find object by seaching the database for its primary data members
  105. {
  106.   StrDB strdb;
  107.  
  108.   strdb.KM = this->KM; 
  109.   strdb.M2 = this->M2;
  110.   strdb.M3 = this->M3;
  111.   strdb.M4 = this->M4;
  112.   strdb.M5 = this->M5;
  113.   strdb.M6 = this->M6;
  114.   strdb.M7 = this->M7;
  115.   strdb.M8 = this->M8;
  116.   strdb.CM = this->CM;
  117.  
  118.   int rv; // Return value
  119.  
  120.   // Search the index file for this entry
  121.   if(UsingIndex()) {
  122.     EntryKey e(this->KM.c_str());
  123.     rv = FindKey(e);
  124.     if(rv) { // Found the object in the index file
  125.       Read(e.object_address);
  126.       return e.object_address;
  127.     }
  128.     else
  129.       return 0;
  130.   }
  131.  
  132.   FAU oa;          // Object Address
  133.   VBHeader vb;     // Variable Block Header
  134.   ObjectHeader oh; // Object Header
  135.   
  136.   FAU vbdfileEOF = pod->OpenDatabase()->GetEOF();
  137.   FAU addr = 0;
  138.   addr = pod->OpenDatabase()->FindFirstVB(addr); // Search the entire file
  139.  
  140.   if(addr == 0) return 0; // No variable blocks found in file
  141.   
  142.   while(1) { 
  143.     if(addr >= vbdfileEOF) break;
  144.     pod->OpenDatabase()->Read(&vb, sizeof(VBHeader), addr);
  145.     if(vb.CkWord == CheckWord) {
  146.       if((__SBYTE__)vb.Status == NormalVB) {
  147.     oa = addr + sizeof(VBHeader);
  148.     ReadObjHeader(oh, oa);
  149.     if(oh.ClassID == GetClassID()) { 
  150.       Read(oa);
  151.       if(KM == strdb.KM) {
  152.         objectaddress = oa;
  153.         return oa; // Found unique data member
  154.       }
  155.     }
  156.       }
  157.       addr = addr + vb.Length; // Goto the next variable block
  158.     }
  159.     else {
  160.       addr = pod->OpenDatabase()->VBSearch(addr); 
  161.       if(!addr) break;
  162.     }
  163.   }
  164.  
  165.   // Reset the objects data
  166.   this->KM = strdb.KM;
  167.   this->M2 = strdb.M2;
  168.   this->M3 = strdb.M3;
  169.   this->M4 = strdb.M4;
  170.   this->M5 = strdb.M5;
  171.   this->M6 = strdb.M6;
  172.   this->M7 = strdb.M7;
  173.   this->M8 = strdb.M8;
  174.   this->CM = strdb.CM;
  175.   
  176.   return 0; // Could not find 
  177. }
  178.  
  179. void StrDB::Copy(const StrDB &ob)
  180. {
  181.   KM = ob.KM;
  182.   M2 = ob.M2;
  183.   M3 = ob.M3;
  184.   M4 = ob.M4;
  185.   M5 = ob.M5;
  186.   M6 = ob.M6;
  187.   M7 = ob.M7;
  188.   M8 = ob.M8;
  189.   CM = ob.CM;
  190.   objectaddress = ob.objectaddress;
  191. }
  192.  
  193. int StrDB::FullCompare(const StrDB &ob)
  194. {
  195.   if(KM != ob.KM) return 0;
  196.   if(M2 != ob.M2) return 0;
  197.   if(M3 != ob.M3) return 0;
  198.   if(M4 != ob.M4) return 0;
  199.   if(M5 != ob.M5) return 0;
  200.   if(M6 != ob.M6) return 0;
  201.   if(M7 != ob.M7) return 0;
  202.   if(M8 != ob.M8) return 0;
  203.   if(CM != ob.CM) return 0;
  204.   return 1;
  205. }
  206.  
  207. int operator==(const StrDB &a, const StrDB &b)
  208. {
  209.   return CaseICmp(a.GetKM(), b.GetKM()) == 0;
  210. }
  211.  
  212. int operator!=(const StrDB &a, const StrDB &b)
  213. {
  214.   return CaseICmp(a.GetKM(), b.GetKM()) != 0;
  215. }
  216.  
  217. int operator>(const StrDB &a, const StrDB &b)
  218. {
  219.   return CaseICmp(a.GetKM(), b.GetKM()) > 0;
  220. }
  221.  
  222. int operator>=(const StrDB &a, const StrDB &b)
  223. {
  224.   return CaseICmp(a.GetKM(), b.GetKM()) >= 0;
  225. }
  226.   
  227. int operator<(const StrDB &a, const StrDB &b)
  228. {
  229.   return CaseICmp(a.GetKM(), b.GetKM()) < 0;
  230. }
  231.   
  232. int operator<=(const StrDB &a, const StrDB &b)
  233. {
  234.   return CaseICmp(a.GetKM(), b.GetKM()) <= 0;
  235. }
  236.  
  237. FAU StrDB::Delete()
  238. {
  239.   if(UsingIndex()) {
  240.     EntryKey e(this->KM.c_str());
  241.     int rv = FindKey(e);
  242.     if(rv) {  // Found the object in the index file
  243.       FAU addr = e.object_address;
  244.       DeleteObject(e.object_address); // Delete from the data file
  245.       RemoveKey(e);                   // Remove from the index file
  246.       return addr;
  247.     }
  248.     else
  249.       return 0; // Could not delete
  250.   }
  251.  
  252.   FAU addr = Find();
  253.   if(!addr) return 0; // Object does not exist
  254.   DeleteObject(addr);
  255.   return addr;
  256. }
  257.  
  258. FAU StrDB::Remove()
  259. {
  260.   if(UsingIndex()) {
  261.     EntryKey e(this->KM.c_str());
  262.     int rv = FindKey(e);
  263.     if(rv) {  // Found the object in the index file
  264.       FAU addr = e.object_address;
  265.       RemoveObject(e.object_address); // Remove from the data file
  266.       RemoveKey(e);                   // Remove from the index file
  267.       return addr;
  268.     }
  269.     else
  270.       return 0; // Could not remove
  271.   }
  272.  
  273.   FAU addr = Find();
  274.   if(!addr) return 0; // Object does not exist
  275.   RemoveObject(addr);
  276.   return addr;
  277. }
  278.  
  279. int StrDB::CompareIndex()
  280. // Compares the data file to the index file.
  281. // Returns true if data and index file match.
  282. {
  283.   if(!UsingIndex()) return 0;
  284.  
  285.   StrDB strdb(pod);
  286.   EntryKey key;
  287.   
  288.   FAU oa;          // Object Address
  289.   VBHeader vb;     // Variable Block Header
  290.   ObjectHeader oh; // Object Header
  291.   
  292.   int objects = 0; // Keeps track of good variable blocks
  293.   int matches = 0; // Keep track of matches
  294.   
  295.   FAU vbdfileEOF = pod->OpenDatabase()->GetEOF();
  296.   FAU addr = 0;
  297.   addr = pod->OpenDatabase()->FindFirstVB(addr); // Search the entire file
  298.  
  299.   if(addr == 0) { // No variable blocks found in file
  300. #ifdef CPP_EXCEPTIONS
  301.     throw CNoObjectsExist();
  302. #else
  303.     Error->SignalException(EHandler::NoObjectsExist, EHandler::DISPLAY);
  304.     return 0;
  305. #endif
  306.   }
  307.   
  308.   while(1) { 
  309.     if(addr >= vbdfileEOF) break;
  310.     pod->OpenDatabase()->Read(&vb, sizeof(VBHeader), addr);
  311.     if(vb.CkWord == CheckWord) {
  312.       if((__SBYTE__)vb.Status == NormalVB) {
  313.     objects++; // Increment t